package com.thh.common.enums;

import com.thh.common.exception.ServiceException;

import javax.xml.bind.annotation.XmlType;

public enum DatabaseDriverEnums {

    MYSQL("mysql", "com.mysql.cj.jdbc.Driver", "show tables like '%s'"),

    ORACLE("oracle", "oracle.jdbc.OracleDriver", "select table_name from all_tables where owner='%s' and table_name = '%s'"),

    H2("h2", "org.h2.Driver", "select table_name from information_schema.tables where table_schema='PUBLIC' and table_name = '%s'");


    private String name;

    private String driverClassName;

    private String checkSql;

    DatabaseDriverEnums(String name, String driverClassName, String checkSql) {
        this.name = name;
        this.driverClassName = driverClassName;
        this.checkSql =  checkSql;
    }

    public String getCheckSql() {
        return checkSql;
    }

    public void setCheckSql(String checkSql) {
        this.checkSql = checkSql;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static DatabaseDriverEnums getByDriverClassName(String driverClassName) {
        DatabaseDriverEnums[] values = DatabaseDriverEnums.values();
        for (DatabaseDriverEnums driver : values) {
            if(driver.getDriverClassName().equals(driverClassName)) {
                return driver;
            }
        }
       throw new ServiceException("没有找到对应的数据源！");
    }

    public static DatabaseDriverEnums getByName(String name) {
        DatabaseDriverEnums[] values = DatabaseDriverEnums.values();
        for (DatabaseDriverEnums driver : values) {
            if(driver.getName().equals(name)) {
                return driver;
            }
        }
        throw new ServiceException("没有找到对应的数据源！");
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }
}
